package yxy.game.pm2.command.therionHunting;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TherionHuntingMessage;
import yxy.game.pm2.module.therionHunting.*;

import java.util.Collection;

/**
 * @name CMD_TherionHuntingRank
 * @author wrs
 * @descrition 神兽猎场排行榜
 * @since 2022-06-10
 */
@MessageType(proto = Command.TherionHuntingRank ,classtype = TherionHuntingMessage.TherionHuntingRankRequest.class)
public class CMD_TherionHuntingRank extends Command<TherionHuntingMessage.TherionHuntingRankRequest> {

    @Override
    public void execute(MessageSource messageSource, TherionHuntingMessage.TherionHuntingRankRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int gameType = request.getGameType();
        int therionId = request.getTherionId(); //神兽ID

        TherionHuntingMessage.TherionHuntingRankResponse.Builder response = TherionHuntingMessage.TherionHuntingRankResponse.newBuilder();

        try {
            TherionHandle handle = new TherionHandle();
            int key = handle.getTherionKey(therionId);

            if (key == -1){
                send(messageSource,response, Result.ParamError);
                return;
            }

            switch (gameType){
                case TherionModule.THERION_LOCAL:
                    AreaRankTherionChallege areaRank = AreaRankTherionChallege.getRank(playerCode, key); //本服
                    Collection<RankEntry> areaTop = areaRank.top(areaRank.topSize());

                    //排行榜信息封装
                    handle.packRank(playerCode,areaTop,areaRank,response);
                    break;

                case TherionModule.THERION_CROSS:
                    AreaGroupRankTherionChallege areaGroupRank = AreaGroupRankTherionChallege.getRank(playerCode, key);//跨服
                    Collection<RankEntry> areaGroupTop = areaGroupRank.top(areaGroupRank.topSize());

                    //排行榜信息封装
                    handle.packRank(playerCode,areaGroupTop,areaGroupRank,response);
                    break;

                case TherionModule.THERION_WORLD:
                    WorldRankTherionChallege worldRank = WorldRankTherionChallege.getRank(playerCode,key);//世界
                    Collection<RankEntry> worldTop = worldRank.top(worldRank.topSize());

                    //排行榜信息封装
                    handle.packRank(playerCode,worldTop,worldRank,response);
                    break;
                default:
                    send(messageSource,response, Result.DataError);
                    break;
            }

            send(messageSource,response, Result.Success);

        }catch (Exception e){
            e.printStackTrace();
            send(messageSource,response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, TherionHuntingMessage.TherionHuntingRankResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
